<template>

  <!--
          作者 luoyiming
          时间：2019-10-26
          描述：设置-物流公司
      -->
  <div class="user">
    <div class="common-form">地區列表</div>
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="名稱"><el-input size="small" v-model="searchForm.name" placeholder="請輸入名稱"></el-input></el-form-item>
        <el-form-item label="地區類型">
          <div>
            <el-select v-model="searchForm.level">
              <el-option label="全部" :value="0"></el-option>
              <el-option label="省份" :value="1"></el-option>
              <el-option label="城市" :value="2"></el-option>
              <el-option label="地區" :value="3"></el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="選擇上級">
          <el-select v-model="searchForm.provinceId" placeholder="省" @change="initCity">
            <el-option label="全部" value=""></el-option>
            <el-option :label="item.name" :value="item.id" v-for="(item,index) in areaList" :key='index'></el-option>
          </el-select>
          <el-select v-if="searchForm.provinceId!=''" v-model="searchForm.cityId" placeholder="市">
            <el-option label="全部" value=""></el-option>
            <el-option :label="item1.name" :value="item1.id" v-for="(item1,index1) in cityList"
              :key='index1'></el-option>
          </el-select>
        </el-form-item>
        <el-form-item><el-button size="large"  type="primary" icon="Search" @click="onSubmit">查詢</el-button></el-form-item>
      </el-form>
    </div>
    <!--添加等级-->
    <div class="common-level-rail">
      <el-button type="primary" @click="addClick">添加</el-button>
    </div>

    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table size="small" :data="tableData" border style="width: 100%" v-loading="loading">
          <el-table-column prop="shortname" label="簡稱"></el-table-column>
          <el-table-column prop="name" label="名稱"></el-table-column>
          <el-table-column prop="mergerName" label="名稱"></el-table-column>
          <el-table-column prop="zipCode" label="郵編"></el-table-column>
          <el-table-column label="級別">
            <template #default="scope">
              <span v-if="scope.row.level == 1">省份</span>
              <span v-if="scope.row.level == 2">城市</span>
              <span v-if="scope.row.level == 3">地區</span>
            </template>
          </el-table-column>
          <el-table-column prop="lng" label="經度"></el-table-column>
          <el-table-column prop="lat" label="緯度"></el-table-column>
          <el-table-column fixed="right" label="操作" width="90">
            <template #default="scope">
              <el-button @click="editClick(scope.row)" type="text" size="small">編輯</el-button>
              <el-button @click="deleteClick(scope.row)" type="text" size="small">刪除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!--分页-->
      <div class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background :current-page="curPage"
          :page-size="pageSize" layout="total, prev, pager, next, jumper"
          :total="totalDataNumber">
        </el-pagination>
      </div>
    </div>

  </div>
</template>

<script>
import RegionApi from '@/api/region.js';
export default {
  data() {
    return {
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 15,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*是否打开添加弹窗*/
      open_add: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*当前编辑的对象*/
      userModel: {},
      searchForm: {
        name: '',
        level: 0,
        provinceId: '',
        cityId: ''
      },
      /*省市区*/
      areaList: [],
      cityList: []
    };
  },
  created() {
    /*获取列表*/
    this.getData();
  },
  methods: {
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getData();
    },
    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getData();
    },
    /*获取列表*/
    getData() {
      let self = this;
      let Params = self.searchForm;
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      RegionApi.regionList(Params, true).then(res => {
        self.loading = false;
        self.tableData = res.data.list.records;
        self.totalDataNumber = res.data.list.total;
        self.areaList = res.data.regionData;
      }).catch(error => {});
    },
    onSubmit() {
      this.curPage = 1;
      this.getData();
    },
    /*打开添加*/
    addClick() {
      this.$router.push('/region/add');
    },
    /*打开编辑*/
    editClick(item) {
      let self = this;
      this.$router.push({
        path: '/region/edit',
        query: {
          id: item.id
        }
      });
    },
    /*初始化城市id*/
    initCity(e) {
      this.searchForm.city_id = '';
      this.areaList.forEach(item => {
        if (item.id == e) {
          this.cityList = item.children;
        }
      });
    },
    /*删除用户*/
    deleteClick(row) {
      let self = this;
      ElMessageBox.confirm("此操作將永久刪除該記錄, 是否繼續?", "提示", {
        confirmButtonText: "確定",
        cancelButtonText: "取消",
        type: 'warning'
      }).then(() => {
        self.loading = true;
        RegionApi.deleteRegion({
          id: row.id
        }, true).then(data => {
          self.loading = false;
          ElMessage({
            message: data.msg,
            type: 'success'
          });
          self.getData();
        }).catch(error => {
          self.loading = false;
        });
      }).catch(() => {});
    }
  }
};
</script>

